package cn.getech.data.development.utils.sap;

import cn.getech.data.development.entity.ConnectFieldInfo;
import cn.getech.data.development.entity.JobNodeConfig;
import cn.getech.data.development.entity.SapConnectField;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import jodd.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * \* AUTH: HQZ
 * \* Date: 2019/10/17
 * \* Time: 11:57
 * \* Description:
 * \
 */
public class SapCheckUtils {

    /**
     * 校验sap数据源配置参数
     * 1.sap数据源的输入参数中的字段名不能为空
     * 2.sap数据源的输入输出参数必须指定数据类型
     */
    public SapConnectField checkSapParams(SapConnectField sapConnectField){
        //校验sap数据源的输入参数中的字段名不能为空
        int sapFieldNum = 0;
        if(null != sapConnectField.getAinputParams() && sapConnectField.getAinputParams().size() == 0){
            /*** sap是可以不用输入参数的 */
           //throw new RRException("sap数据源中必须有输入参数");
        }
        if(sapConnectField.getAinputParams()!=null&&sapConnectField.getAinputParams().size()>0){
            for(JSONObject j : sapConnectField.getAinputParams()){
                if(null != j.get("fieldInputType") && StringUtil.equals("2",String.valueOf(j.get("fieldInputType")))){
                    sapFieldNum++;
                }
            }
        }
        if(sapFieldNum <= 0){
          //throw new RRException("sap数据源中必须有字段类型的输入参数");
        }
        if(sapConnectField.getOutputTableName() == null || StringUtil.equals("",sapConnectField.getOutputTableName())){
            throw new RRException("sap数据源输出参数的表名不能为空");
        }
        String[] sapParams = {"fieldName","fieldKind","fieldValue","fieldAlias","fieldInputType","fieldDataType"};
        addNullForEmptyAndCheck(sapConnectField, sapParams);
        return sapConnectField;
    }

    public SapConnectField checkWebServiceParams(JobNodeConfig jobNodeConfig, List<ConnectFieldInfo> list)throws Exception{
        SapConnectField sapConnectField = new SapConnectField();
        SapConnectField sapConnectField2 = jobNodeConfig.getSapConnectFieldInfo();
        if (CollectionUtil.isEmpty(list)){
            sapConnectField2.setAinputParams(null);
        }else{
            if (CollectionUtil.isEmpty(sapConnectField2.getAinputParams())){
                throw new RRException("请配置数据源的输入参数");
            }
            else if (sapConnectField2.getAinputParams().size()!=list.size()){
                throw new RRException("输入参数个数与数据源不一致");
            }
            String[] params = {"fieldName","fieldValue","fieldAlias"};
            addNullForEmptyAndCheck(sapConnectField2,params);
        }
        sapConnectField.setAinputParams(sapConnectField2.getAinputParams());
        return sapConnectField;
    }

    /**
     * 补全sap前端数据中的空值为字符串null
     * @param sapConnectField
     * @return
     */
    public SapConnectField addNullForEmptyAndCheck(SapConnectField sapConnectField,String[] params){

        List<JSONObject> ainputParams = sapConnectField.getAinputParams();
        List<JSONObject> aoutputParams = sapConnectField.getAoutputParams();
        List<List<JSONObject>> list = new ArrayList<>();
        list.add(ainputParams);
        if(null != aoutputParams && !aoutputParams.isEmpty()){
            list.add(aoutputParams);
        }
        /**
         * fieldAlias: "DAF"
         * fieldDataType: "num"
         * fieldInputType: 2
         * fieldKind: "1"
         * fieldName: "PIVZMONTH"
         * fieldValue: "20200722"
         */
        for(List<JSONObject> l : list){
            for(JSONObject j : l){
                for(String s : params){
                    if(null == j.get(s) || StringUtil.equals("", String.valueOf(j.get(s)))){
                        if(s.equals("fieldValue")) {
                            j.put(s,"");
                        }
                        if(s.equals("fieldInputType")) {
                            j.put(s,2);
                        }if(s.equals("fieldKind")) {
                            j.put(s,"1");
                        }if(s.equals("fieldDataType")) {
                            j.put(s,"num");
                        }
                    }
                }
            }
        }
        sapConnectField.setAinputParams(list.get(0));
        if(null != aoutputParams && !aoutputParams.isEmpty()){
            sapConnectField.setAoutputParams(list.get(1));
        }
        return sapConnectField;
    }
}
